//
//  AppDelegate.m
//  Scanner
//
//  Created by 米明 on 15/5/14.
//  Copyright (c) 2015年 米明. All rights reserved.
//

#import "AppDelegate.h"
//AFNetWorking网络监测
#import "AFNetworkReachabilityManager.h"
#import "SDImageCache.h"
#import "AFNetworkActivityIndicatorManager.h"
#import "PhotosUploadManager.h"
@interface AppDelegate ()
{
    //网络监测结果是否显示过?
    BOOL _showed;
    
}
@property(nonatomic,retain)UIAlertView * alertView;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //设置图片缓存地址
    NSString *bundledPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CustomPathImages"];
    
    [[SDImageCache sharedImageCache] addReadOnlyCachePath:bundledPath];
    
    //网络监测
    [self reachNetWorkStatus];
    
    [self AFNetWorkingEnable];
    
    self.window.backgroundColor=[UIColor whiteColor];
    return YES;
}
#pragma mark - AFNetWorking设置可用性
-(void)AFNetWorkingEnable{
    
    NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
    
    [NSURLCache setSharedURLCache:URLCache];
    
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
}
#pragma mark - 网络监测
-(void)reachNetWorkStatus{
    
    /**
     AFNetworkReachabilityStatusUnknown          = -1,  // 未知
     AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接
     AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G 花钱
     AFNetworkReachabilityStatusReachableViaWiFi = 2,   // 局域网络,不花钱
     */
    
    // 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        //NSLog(@"%d", (int)status);
        NSString * reachResultInfo;
        
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
            case AFNetworkReachabilityStatusNotReachable:
            {
                if (_showed==NO) {
                    _showed=YES;
                  
                    _alertView =[[UIAlertView alloc]initWithTitle:@"未连接网络,请检查网络设置" message:nil delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
                    [_alertView show];
                    
                    //[[PhotosUploadManager manager]setup];
                }
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
            {
                reachResultInfo=@"2G/3G";
                
                if(_showed==YES){
                  
                    _alertView =[[UIAlertView alloc]initWithTitle:@"当前网络为蜂窝网络" message:nil delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
                    [_alertView show];
                    
                    _showed=NO;
                    
                
                }
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {
                reachResultInfo=@"Wifi";
                if(_showed==YES){
                    _alertView =[[UIAlertView alloc]initWithTitle:@"当前网络为WiFi" message:nil delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
                    [_alertView show];
                    _showed=NO;
                    
            
                }
            }
                break;
            default:
                break;
        }
        
    }];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
